<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/10 0010
 * Time: 下午 5:28
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class User_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 查询用户数据
     * */
    public function checkAll($limit,$offset)
    {
        return $this->db->limit($limit,$offset)->get('user')->result_array();
    }

    /**
     * 查询总行数
     * */
    public function countRows()
    {
        return $this->db->count_all('user');
    }

    /**
     * 添加新用户
     * */
    public function addUser($data)
    {
        //获取地址
        $this->load->model('Area_model');
        $province = $this->Area_model->areaName($areaid=$data['province']);
        $city = $this->Area_model->areaName($areaid=$data['city']);
        $country = $this->Area_model->areaName($areaid=$data['country']);
        //echo $province.$city.$country;die;
        //echo $data['myfile'];die;
        $new_data = [
          'username'=>$data['username'],
          'userpwd'=>$data['pwd'],
          'head_img'=>$data['myfile'],
          'user_address'=>$province.','.$city.','.$country,
          'phone'=>$data['mobile'],
          'register_time'=>time()
        ];
        $res = $this->db->insert('user',$new_data);
        return $res;
    }

    /**
     *删除用户
     * */
    public function delUser($id)
    {
        return $this->db->delete('user',['user_id'=>$id]);
    }

    /**
     * 提示信息
     * */
    public function message($res,$success_msg='ok',$false_message='no')
    {
        if($res)
        {
            $ret = ['status'=>'success','message'=>$success_msg];
        }
        else
        {
            $ret = ['status'=>'false','message'=>$false_message];
        }
        return json_encode($ret);
    }


    /**
     * 前台用户注册
     * */
    /**
     * 前台添加新用户
     * */
    public function faceAddUser($data)
    {

        $new_data = [
            'userpwd'=>md5($data['pwd']),
            'username'=>$data['username'],
            'phone'=>$data['mobile'],
            'register_time'=>time()
        ];
        $res = $this->db->insert('user',$new_data);
        return $res;
    }

    /**
     *前台登录
     * */
    public function doLogin($data)
    {
        $user_data = ['phone'=>$data['mobile'],'userpwd'=>md5($data['password'])];
        $res = $this->db->where($user_data)->get('user')->row_array();
        return $res;
    }

}